package com.qqtech.qquan.hi.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.agent.context.AdminPage;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.hi.model.HiUpgrade;
import com.qqtech.qquan.hi.model.HiUpgradeQuery;
import com.qqtech.qquan.hi.model.HiUpgradeVo;
import com.qqtech.qquan.hi.service.HiUpgradeService;
import com.qqtech.qquan.sys.controller.BaseAgentController;

@Controller
@RequestMapping(value = "/hi/upgrade")
public class HiUpgradeController extends BaseAgentController {

	@Resource
	private HiUpgradeService hiUpgradeService;

	@Override
	protected BaseService<HiUpgrade> getBaseService() {
		return hiUpgradeService;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		return "/hi/hiUpgrade-list";
	}

	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(HiUpgradeQuery query, HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		query.setStatus(YesNoEnum.YES.getCode());
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(), new PageOrder(Direction.DESC, "id"));
		Page<HiUpgradeVo> page = hiUpgradeService.queryPageList(query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}

	/**
	 * 去新增
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public String toAdd(Model model) {
		return "/hi/hiUpgrade-add";
	}

	/**
	 * 创建成功
	 * 
	 * @param add
	 * @return
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Result add(HiUpgrade hiUpgrade) {
		Result ret = new Result();
		hiUpgrade.setStatus(YesNoEnum.YES.getCode());
		hiUpgradeService.insert(hiUpgrade);
		return ret;
	}

	/**
	 * 去更新
	 * 
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/update")
	public String toUpdate(Integer id, Model model) {
		HiUpgradeVo update = hiUpgradeService.getById(id);
		model.addAttribute("update", update);
		return "/hi/hiUpgrade-update";
	}

	/**
	 * 删除
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Result delete(HiUpgrade hiUpgrade) {
		Result ret = new Result();
		hiUpgrade.setStatus(YesNoEnum.NO.getCode());
		hiUpgradeService.updateById(hiUpgrade);
		return ret;
	}

	/**
	 * 创建成功
	 * 
	 * @param shareId
	 * @return
	 */
	@RequestMapping(value = "/doUpdate")
	@ResponseBody
	public Result update(HiUpgrade hiUpgrade) {
		Result ret = new Result();
		hiUpgradeService.updateById(hiUpgrade);
		return ret;
	}
}
